package cn.tedu.property_management_system.utils;

import cn.tedu.property_management_system.ex.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import static cn.tedu.property_management_system.utils.JsonResult.State.UNAUTHORIZED;

@Slf4j
public class SecurityUtil {
    public static String getConcurrentUserName(){
        Authentication authentication = SecurityContextHolder
                .getContext().getAuthentication();
        if (authentication!=null){
            String username = (String)authentication.getPrincipal();
            log.debug("获取到当前用户名为:[{}]",username);
            return username;
        }
        throw new ServiceException(UNAUTHORIZED,"请先登录");
    }
}
